<template>
  <div>
    <table>
      <caption>
        水果店
      </caption>
      <thead>
        <tr>
          <th>苹果 10 ￥ / 斤，折扣 《 8 折 》</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>请输入您要购买的斤数 <input type="number" v-model="weight" /></td>
        </tr>
        <tr>
          <td><button @click="resFn">结 账</button></td>
        </tr>
      </tbody>
      <tfoot>
        <tr>
          <td>
            账单：总价：{{ weight }} ￥元，折后价：{{ factPrice }} ￥元，省了：{{ savePrice }} ￥元
          </td>
        </tr>
      </tfoot>
    </table>
  </div>
</template>
 
<script>
export default {
  data() {
    return {
      weight: 0,
      sumPrice: 0,
      factPrice: 0,
      savePrice: 0
    }
  },
  methods: {
    resFn() {
      if (this.weight < 0) return alert('购买斤数不能为负！！')
      this.sumPrice = this.weight * 10
      this.factPrice = this.sumPrice * 0.8
      this.savePrice = this.sumPrice - this.factPrice
    }
  }
}
</script>

<style scoped>
table {
  width: 600px;
  height: 100px;
  padding: 20px;
  text-align: center;
  line-height: 30px;
  border: 1px solid #000;
  border-collapse: collapse;
}
table tr td {
  border: 1px solid #000;
}
</style>